//
//  TestViewController.m
//  图片移动
//
//  Created by 小果 on 16/7/3.
//  Copyright © 2016年 小果. All rights reserved.
//
/**
 *******************************************************
 type:动画类型(比如：滴水效果，翻转效果...)
 -------------------------------------------------------
 fade kCATransitionFade 交叉淡化过渡
 moveIn kCATransitionMoveIn 新视图移到旧视图上面
 push kCATransitionPush 新视图把旧视图推出去
 reveal kCATransitionReveal 将旧视图移开,显示下面的新视图
 pageCurl               向上翻一页
 pageUnCurl             向下翻一页
 rippleEffect             滴水效果
 suckEffect 收缩效果，如一块布被抽走
 cube                   立方体效果
 oglFlip              上下左右翻转效果
 rotate     旋转效果
 cameraIrisHollowClose 相机镜头关上效果(不支持过渡方向)
 cameraIrisHollowOpen 相机镜头打开效果(不支持过渡方向)
 
 *******************************************************
 subtype: 动画方向(比如说是从左边进入，还是从右边进入...)
 ------------------------------------------------------
 kCATransitionFromRight;
 kCATransitionFromLeft;
 kCATransitionFromTop;
 kCATransitionFromBottom;
 
 当 type 为@"rotate"(旋转)的时候,它也有几个对应的 subtype,分别为:
 90cw 逆时针旋转 90°
 90ccw 顺时针旋转 90°
 180cw 逆时针旋转 180°
 180ccw  顺时针旋转 180°
 **/

#import "TestViewController.h"

@interface TestViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIImageView *imgView;

@property (nonatomic, strong) UILabel *label;
@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    self.scrollView.delegate = self;
    self.scrollView.center = self.view.center;
    self.scrollView.minimumZoomScale = 0.6;
    self.scrollView.maximumZoomScale = 2.0;
    [self.view addSubview:self.scrollView];
    
    
    self.imgView = [[UIImageView alloc] init];
    self.imgView.frame = CGRectMake(0, 0, 300, 300);
    self.imgView.center = self.view.center;
    self.imgView.backgroundColor = [UIColor brownColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self.imgView action:@selector(startAnimation:)];
    [self.imgView addGestureRecognizer:tap];
//    self.imgView.contentMode = UIViewContentModeScaleAspectFill;
    self.imgView.image = self.img;
    [self.view addSubview:self.imgView];
    

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
    self.label.center = self.imgView.center;
    self.label.font = [UIFont systemFontOfSize:27];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.textColor = [UIColor redColor];
    self.label.userInteractionEnabled = YES;
    UITapGestureRecognizer *labelClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(startsAnimation)];
    [self.label addGestureRecognizer:labelClick];
    self.label.text = self.st;
    [self.view addSubview:self.label];
    
}

-(void)startsAnimation{
    CATransition *anim = [CATransition animation];
    //    anim.type = @"rotate";
    //    // subtype 不一定每个转场动画的都要
    //    anim.subtype = @"90ccw";
    
    //    anim.type = @"rippleEffect";
//    anim.type = @"cameraIrisHollowClose";
    anim.type = @"reveal";
    anim.repeatCount = 2;
    anim.duration = 1;
    [self.imgView.layer addAnimation:anim forKey:nil];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self startAnimation:self.imgView];
}
-(void)startAnimation:(UIImageView *)image{
    
    CATransition *anim = [CATransition animation];
//    anim.type = @"rotate";
//    // subtype 不一定每个转场动画的都要
//    anim.subtype = @"90ccw";

//    anim.type = @"rippleEffect";
    anim.type = @"cube";
    anim.repeatCount = 2;
    anim.duration = 1;
    [image.layer addAnimation:anim forKey:nil];

}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.imgView;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    
    [UIView animateWithDuration:1.0 animations:^{
        self.imgView.frame = CGRectMake(0, 0, 300, 300);
        self.imgView.center = self.view.center;
    }];
}
@end
